<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
require('echarts/lib/component/legend')

import { debounce } from '@/utils'

export default {
  name: 'Googleadschart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      adnotes: [],
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (this.chart) {
          this.setOptions(val)
        } else {
          this.chart = echarts.init(this.$el, 'macarons')
          this.setOptions(this.chartData)
        }
      }
    }
  },
  mounted() {
    // this.initChart()
    if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    }

    // 监听侧边栏的变化
    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHanlder)
    }

    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions({ xAxis, clicks, display, cpc, roi, cost, amount } = {}) {
      this.chart.setOption({
        xAxis: {
          data: xAxis,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          formatter: function(params, ticket, callback, addnotes) {
            let htmlStr = ''
            for (let i = 0; i < params.length; i++) {
              const param = params[i]
              const xName = param.name// x轴的名称
              const seriesName = param.seriesName// 图例名称
              const value = param.value// y轴值
              const color = param.color// 图例颜色

              if (i === 0) {
                htmlStr += xName + '<br/>'// x轴的名称
                if (cpc[xName] !== '') {
                  htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:#FFA500' + ';"></span>'
                  htmlStr += 'CPC:'
                  htmlStr += cpc[xName] + '<br/>'// x轴的名称
                }
                if (cpc[xName] !== '') {
                  htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:#228B22' + ';"></span>'
                  htmlStr += 'ROI:'
                  htmlStr += roi[xName] + '<br/>'// x轴的名称
                }
              }
              htmlStr += '<div>'
              // 为了保证和原来的效果一样，这里自己实现了一个点的效果
              htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + color + ';"></span>'
              htmlStr += seriesName + '：' + value
              htmlStr += '</div>'
            }
            return htmlStr
          },
          padding: [5, 10]
        },
        legend: {
          data: ['展示', '点击', '花费', '销售']
        },
        yAxis: [
          { // 第一个y轴位置在左侧
            position: 'left',
            type: 'value',
            name: '展示',
            axisLine: {
              lineStyle: {
                color: '#FF005A'
              }
            },
            axisLabel: {
              formatter: '{value} 次'
            }
          },
          { // 第二个y轴在右侧
            position: 'left',
            type: 'value',
            name: '点击',
            axisLine: {
              lineStyle: {
                color: '#3888fa'
              }
            },
            axisLabel: {
              formatter: '{value} 次'
            }
          },
          { // 第三个y轴也在右侧，距第二个70个像素
            position: 'right',
            offset: 70,
            type: 'value',
            name: '花费',
            axisLine: {
              lineStyle: {
                color: '#9400D3'
              }
            },
            axisLabel: {
              formatter: '{value}美元'
            }
          },
          { // 第三个y轴也在右侧，距第二个70个像素
            position: 'right',
            offset: 70,
            type: 'value',
            name: '销售',
            axisLine: {
              lineStyle: {
                color: '#00CDCD'
              }
            },
            axisLabel: {
              formatter: '{value}美元'
            }
          }
        ],

        series: [{
          name: '展示', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          yAxisIndex: '0',
          data: display,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '点击',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          yAxisIndex: '1',
          data: clicks,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },
        {
          name: '花费',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#9400D3',
              lineStyle: {
                color: '#9400D3',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          yAxisIndex: '2',
          data: cost,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },
        {
          name: '销售',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#00CDCD',
              lineStyle: {
                color: '#00CDCD',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          yAxisIndex: '3',
          data: amount,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }
        ]
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    }
  }
}
</script>
